unit frmEstado;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, frmCadModelo, DB, MDOCustomDataSet, MDOQuery, StdCtrls, Buttons,
  Grids, DBGrids, ComCtrls, jpeg, ExtCtrls, frmFunctions;

type
  TF_Estado = class(TF_)
    EditDescricao: TEdit;
    EditCod: TEdit;
    EditIBGE: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    QryAux: TMDOQuery;
    procedure dbConsDblClick(Sender: TObject);
    procedure btnOkClick(Sender: TObject);
    procedure btnExcluirClick(Sender: TObject);
    procedure EditConsKeyPress(Sender: TObject; var Key: Char);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  F_Estado: TF_Estado;

implementation

uses frmDMGeral;

{$R *.dfm}

procedure TF_Estado.dbConsDblClick(Sender: TObject);
begin
  inherited;
  with QryCons do begin
    EditCod.Text       := FieldByName('EST_EST').AsString;
    EditIBGE.Text      := FieldByName('EST_IBG').AsString;
    EditDescricao.Text := FieldByName('EST_NOM').AsString;
  end;
  pgDados.ActivePage := tsDados;
end;

procedure TF_Estado.btnOkClick(Sender: TObject);
var
  nEdita :Integer;
  campos, valores :String;
begin
  inherited;
  nEdita := 0;
  buscarEsp('EST_NOM', QryAux, sNomeTab, 'EST_EST', EditCod.Text,'',2);
  QryAux.First;
  if not QryAux.Eof then
    nEdita := 1;
  if nEdita = 0 then begin
    campos  := 'EST_EST, EST_NOM, EST_IBG';
    valores := valores + QuotedStr(EditCod.Text)+', ';
    valores := valores + QuotedStr(EditDescricao.Text)+', ';
    valores := valores + QuotedStr(EditIBGE.Text);
    if insere(QryDados, campos, valores, sNomeTab) then
      mensagens(0)
    else
      mensagens(1);
   end else begin
    valores :=' EST_NOM = '+QuotedStr(EditDescricao.Text);
    valores := Valores + ' , EST_IBG = '+QuotedStr(EditIBGE.Text);
    campos := 'EST_EST = '+QuotedStr(EditCod.Text);
    if altera(QryDados, sNomeTab, valores, campos)then
      mensagens(2)
    else
      mensagens(3);
  end;
  
  LimparCampos(self);
  RefreeshTab(QryCons);
end;

procedure TF_Estado.btnExcluirClick(Sender: TObject);
begin
  inherited;
  if Empty(EditCod.Text) then begin
    ShowMessage('Infome o estado!');
    Abort;
  end;

  if remove(QryAux,sNomeTab,'EST_EST', QuotedStr(EditCod.Text)) then begin
    mensagens(4);
    LimparCampos(Self);
    RefreeshTab(QryCons);
  end else
    mensagens(5);
end;

procedure TF_Estado.EditConsKeyPress(Sender: TObject; var Key: Char);
var
  campos: String;
begin
  inherited;
  campos := ' EST_EST, EST_NOM, EST_IBG ';
  buscarEsp(campos, QryCons, sNomeTab, 'EST_NOM', EditCons.Text, 'EST_NOM',3);
end;

procedure TF_Estado.FormCreate(Sender: TObject);
var
  campos: String;
begin
  inherited;
  QryCons.Close;
  campos := ' EST_EST, EST_NOM, EST_IBG ';
  buscarEsp(campos, QryCons, 'ESTADO', '', '', 'EST_NOM',4);
end;

procedure TF_Estado.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  F_Estado := nil;
end;

end.
